
///
/// 张诚宇
///
/// 登陆页面
///
/// 2022.6.14
///
///
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:zhangchengyu20220614/model/LoginEntity.dart';
import 'package:zhangchengyu20220614/model/httputils.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({Key key}) : super(key: key);

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {

  TextEditingController _controller;
  TextEditingController _controller2;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller=TextEditingController();
    _controller2=TextEditingController();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _controller.dispose();
    _controller2.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(onPressed: (){
          Navigator.pop(context);
        },icon: Icon(Icons.close)),
      ),
      body: Column(
        children: [
          Image.asset('assets/images/zwpic.jpeg',width: 80,height: 80,),
          SizedBox(height: 15,),
          TextField(
            controller: _controller,
            decoration: InputDecoration(
              hintText: '请输入账号',
            ),
          ),
          TextField(
            controller: _controller2,
            decoration: InputDecoration(
              hintText: '请输入密码',
            ),
          ),
          SizedBox(height: 10,),
          Container(
            width: MediaQuery.of(context).size.width-30,
            height: 50,
            child: RaisedButton(onPressed: (){
              NetWorkManaget().netWorkPost('http://mobile.bwstudent.com/hotNews/user/v1/login',map2: {'userName':_controller.text,'passwd':_controller2.text},onSuccess: (data){
                if(data['status']=='0000'){
                  setSpData(LoginEntity.fromJson(data).result);
                }
              });
            },child: Text('登录'),),
          ),
        ],
      ),
    );
  }

  void setSpData(Result result) async{
    var sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.setBool('isLogin', true);
    sharedPreferences.setInt('userId', result.userId);
    sharedPreferences.setString('token', result.token);
    var json = jsonEncode(result);
    sharedPreferences.setString('userMsg', json);
    Fluttertoast.showToast(msg: '登录成功');
    Navigator.pop(context,true);
  }
}
